import { ElMessage } from 'element-plus'
function numFormat(value: number): string {
    return value > 9 ? value + '' : '0' + value;
}
function timeFormat(date: number): string {
    if (!date || +date === 0) {
        return '--';
    }
    const dateFormat = new Date(date * 1000);
    const yy = dateFormat.getFullYear();
    const mm = numFormat(dateFormat.getMonth() + 1);
    const dd = numFormat(dateFormat.getDate());
    const hh = numFormat(dateFormat.getHours());
    const min = numFormat(dateFormat.getMinutes());
    const ss = numFormat(dateFormat.getSeconds());
    return yy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;
}
function objFormat(obj: any): any {
    const pro = [
        'stime',
        'utime',
        'etime',
        'publish_time',
        'unpublish_time',
        'ctime'
    ];
    pro.forEach((item: string) => {
        obj[item] && (obj[item] = timeFormat(obj[item]));
    });
    return obj;
}
function dataFormat(data: any) :any {
    if(Array.isArray(data)){
        return data.map(item =>{
            if (typeof item === 'object') {
                return objFormat(item);
            } else {
                return item;
            }
        })
    }else{
        const pro = ['stime', 'etime', 'publish_time', 'unpublish_time'];
        pro.forEach((item: string) => {
            data[item] && (data[item] = Math.round(data[item] / 1000) + '');
        });
        return data;  
    }
}
function dateFormat(data: any): any {
    if (Array.isArray(data)) {
        return data.map(item => {
            if (typeof item === 'object') {
                return objFormat(item);
            } else {
                return item;
            }
        });
    } else {
        const pro = ['stime', 'etime', 'publish_time', 'unpublish_time'];
        pro.forEach((item: string) => {
            data[item] && (data[item] = data[item] * 1000);
        });
        return data;
    }
}
function urlFormat(path: string): string {
    return path
        ? process.env.VUE_APP_IMA + path
        : '';
}
async function downloadRes(url: string, name: string) {
    const response = await fetch(urlFormat(url)); // 内容转变成blob地址
    const blob = await response.blob(); // 创建隐藏的可下载链接
    const objectUrl = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = objectUrl;
    a.download = name + '.xls';
    a.click();
    a.remove();
}
function statusFormat(st: string, et: string): number {
    const now = new Date();
    return now < new Date(st) ? 1 : now > new Date(et) ? 3 : 2;
}

function statusValue (value: number) : string {
    return value === 1 ? '正常' : '禁用'
}
function formatFileSizes(size) {
    const scale = 1000
    let _integer = size/scale //最小单位kb
    while(_integer > scale) {
      _integer = Math.round(_integer/scale)
    }
    return `${_integer.toFixed(2)}`
}
function changeMBtoGB(num) {
    if (num < 1024) {
        return num+'MB'
    } else {
        return (num / 1024).toFixed(2) + 'GB'
    }
}
const formatFileSize = size => {
    const scale = 1000
    const digitList = ['MB', 'GB', 'TB']
    let _integer = size/scale //最小单位kb
    let digit = 0
    while(_integer > scale) {
      _integer = Math.round(_integer/scale)
      digit++
    }
    return `${_integer.toFixed(2)}${digitList[digit]}`
  }
function formatNumber(num) {
    return num.toString().replace(/\d+/, function (n) {
        return n.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    })
}
function HourFormat(date: number): string {
    const hh = (date / 1000 / 60 / 60 % 24);
    const min = (date / 1000 / 60 % 60)
    const sec = (date / 1000 % 60)
    let hours = parseInt(hh.toFixed(2));  //时
    let minutes = parseInt(min.toFixed(2));  //分
    let seconds = parseInt(sec.toFixed(2));  //秒
    return hours + '时' + minutes + '分钟';
}
function copyFn(value: string){
    var input = document.createElement("input"); // 创建input对象
    input.value = value; // 设置复制内容
    document.body.appendChild(input); // 添加临时实例
    input.select(); // 选择实例内容
    document.execCommand("Copy"); // 执行复制
    document.body.removeChild(input); // 删除临时实例
    ElMessage.success('复制成功')
}

function liveStatus(type: string) {
    if(type === 'Publishing') {
        return '直播中'
    } else if ( type === 'Ready') {
        return '已就绪'
    } else if ( type === 'Forbided') {
        return '禁播'
    } else {
        return '不存在'
    }
}
export default app => {
    app.config.globalProperties.$utils = {
        timeFormat,
        dateFormat,
        dataFormat,
        urlFormat,
        downloadRes,
        statusFormat,
        statusValue,
        changeMBtoGB,
        formatFileSize,
        formatFileSizes,
        formatNumber,
        HourFormat,
        copyFn,
        liveStatus
    };
};